package com.example.demo6.controller;

import com.example.demo6.domain.Movie;
import com.example.demo6.domain.Role;
import com.example.demo6.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *电影控制器类
 */
@Controller
@RequestMapping(value = "/movie")
public class MovieController {

    @Autowired//自动注入
    private MovieRepository movieRepository;

    //http://localhost:8088/movie/show/{id}
    @RequestMapping(value ="/show/{id}" )
    @ResponseBody
    public Movie show(@PathVariable Long id){
        Movie movie = movieRepository.findOne(id);

        System.out.println("电影名称：" + movie.getName());
        //
        for (Role role: movie.getRoles()) {
            System.out.println("角色名称：" + role.getName() + "， 扮演者:" + role.getActor().getName());
        }
        return movie;

    }

    @RequestMapping("/detail/{id}")
    public String detail(Model model, @PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);
        return "movie/detail";
    }

    @RequestMapping(value = {"/index","","/"})
    public String index(Model model){
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list", list);
        return "movie/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd(){
        return "movie/add";
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam String name, @RequestParam String photo, @RequestParam String createDate){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        movieRepository.save(movie);
        System.out.println("这是MovieController 的doAdd方法, name = " + name + ", photo = " + photo + ", createDate = " + createDate);
        return "redirect:/movie/index";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);
        return "movie/edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id,@RequestParam String name, @RequestParam String photo, @RequestParam String createDate){


        //通过Id获取电影信息
        Movie movie = movieRepository.findOne(id);

        //就修改的内容赋值给电影movie对象
        //将发行日期字符串转换成日期类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        //赋值
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        //执行保存动作
        movieRepository.save(movie);

        //重定向到电影详情页面
        return "redirect:/movie/detail/" + movie.getId();
    }


    /**
     * 通过电影Id删除电影信息
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable Long id) {
        movieRepository.delete(id);
        return "redirect:/movie/index";
    }


}
